package com.woniu.apiImpl;
import com.woniu.api.DrugsApi;
import com.woniu.dto.DrugsDto;
import com.woniu.dto.DrugsPageDto;
import com.woniu.es.repo.DrugsRepo;
import com.woniu.model.HisMedicine;
import com.woniu.param.DeleteDrugsByListIdParam;
import com.woniu.param.DrugsParam;
import com.woniu.param.QueryDrugsPageParam;
import com.woniu.service.IHisMedicineService;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import com.woniu.utils.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/drugs")
@Slf4j
public class HisMedicineApiImpl implements DrugsApi {

   @Resource
    private DrugsRepo drugsRepo;

   @Resource
   private IHisMedicineService iHisMedicineService;

    @Override
    public Result<DrugsPageDto> getDrugs(QueryDrugsPageParam queryDrugsPageParam)  {

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        if (queryDrugsPageParam.getQueryDrugsParam().getMedicineName()!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("medicineName", queryDrugsPageParam.getQueryDrugsParam().getMedicineName()));
        }
        if (queryDrugsPageParam.getQueryDrugsParam().getMedicineForm()!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("medicineForm",queryDrugsPageParam.getQueryDrugsParam().getMedicineForm()));
        }

        queryBuilder.withPageable(PageRequest.of(queryDrugsPageParam.getPageParam().getPage().intValue()-1,queryDrugsPageParam.getPageParam().getPageSize().intValue()));
        Page<HisMedicine> search = drugsRepo.search(queryBuilder.build());
        //总条数
        Long totalElements = search.getTotalElements();
        //总页数
        Integer totalPages = search.getTotalPages();

        //分页后list
        List<HisMedicine> content = search.getContent();



        List<DrugsDto> drugsDtos = CopyBeanUtil.copyBean(content, DrugsDto.class);
        DrugsPageDto drugsPageDto = new DrugsPageDto();
        drugsPageDto.setList(drugsDtos);
        drugsPageDto.setPagesSize(totalElements);
        drugsPageDto.setPages(totalPages);
        return Result.bulid(drugsPageDto);
    }

    @Override
    public Result<DrugsDto> getDrugsById(Integer medicineId) {

        DrugsDto drugsByid = iHisMedicineService.getDrugsByid(medicineId);

        return Result.bulid(drugsByid);
    }

    @Override
    public Result<DrugsDto> addDrugs( DrugsParam drugsParam) {
        ParamUtil.bulidMust(drugsParam,"medicineName","medicineSpecifications","approvalNumber");
        DrugsDto drugsDto = iHisMedicineService.AddDrugs(drugsParam);
        return Result.bulid(drugsDto);
    }

    @Override
    public Result<?> DeleteDrugs(DeleteDrugsByListIdParam deleteDrugsParamByListId) {
        Boolean aBoolean = iHisMedicineService.DelDrugs(deleteDrugsParamByListId);

        return Result.bulid().setFlag(aBoolean);
    }

    @Override
    public Result<DrugsDto> updateDrugs(DrugsParam drugsParam) {
        DrugsDto drugsDto = iHisMedicineService.UpdateDrugs(drugsParam);

        return Result.bulid(drugsDto);
    }
}

